package mrt.cse.tempworks.initialWorksOfTeamMembers.shameera;

import org.apache.zookeeper.data.Stat;

import java.util.List;


public class Manager extends NodeWatcher implements Runnable{
    public static int count =0;


    public Manager(String name) {
        this.name= name;
    }

    
    public void invoke(){

        Stat stat= isNodeExist(defPath, true);
        if(stat!=null){
            createZnode(defPath+"/"+name , name);
            List<String> childList = getChildernOfMainNode();
            if (childList.size()==1) {
                assignLeaderMySelf(childList.get(0) , getZidOfNode(childList.get(0)));

            }else{
                processForLeader(childList);
            }

        }

    }

    public void startMan(){
        connectToServer("localhost");
        System.out.println(name + "  started");
        invoke();
        while(true){

        }
    }

    @Override
    public void run() {
        connectToServer("localhost");
        System.out.println(name + "  started");
        invoke();
        while(true){}
/*        boolean process = true;
        count++;
        while(process){
           if(count==2 && name.equalsIgnoreCase("sham")){
              for(int i =0; i < 100; i++){

              }
               process=false;
               System.out.println("Sham DEAD");
           }
        }*/
    }
}
